<?php

namespace App\Http\Controllers\back;

use App\Model\press;
use App\Model\presse;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class PressController extends Controller
{

    //出版社管理
    public function newpress()
    {
//        $all=presse::all();
        $all=DB::table('presses')->paginate(4);
        return view('back.presss.newpresss',compact('all'));
    }

    //新增出版社
    public function addpresss(Request $request)
    {
        if ($request->isMethod('post')){
            $add=new presse();
            $add->name=$request->name;
            $add->http=$request->http;
            $add->desc=$request->description;
            $add->save();
            $id=$add->id;
            $dir=public_path().'/image/presss/';
            $name=$id.'.jpg';

            $request->file0->move(public_path().'/image/presss/',$id.'.jpg');

            $icon=presse::find($id);
            $icon->pressicon=$name;
            $icon->save();
            return redirect('back/presss/newpresss');
        }
        return view('back.presss.addpresss');
    }

    //修改出版社
    public function uppresss(Request $request,$id)
    {
        if ($request->isMethod('post')){
            $uppre=presse::find($id);
            $uppre->name=$request->name;
            $uppre->http=$request->http;
            $uppre->addtime=time();
            $uppre->save();
            if(!empty($request->file0)){
                unlink(public_path().'/image/presss/'.$id.'.jpg');
                $request->file0->move(public_path().'/image/presss/',$id.'.jpg');
            }
            return redirect('back/presss/newpresss');
        }
        $up=presse::find($id);
        return view('back.presss.uppresss',compact('up'));
    }

    //删除出版社
    public function delpresss($id)
    {
        $del=presse::find($id);
        $del->delete();
        unlink(public_path().'/image/presss/'.$id.'.jpg');
        return redirect('back/presss/newpresss');
    }

}
